package com.chenxingnet.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chenxingnet.dto.Department;
import com.chenxingnet.dto.Publicity;
import com.chenxingnet.dto.Remind;
import com.chenxingnet.dto.vo.meetVo;
import com.chenxingnet.service.DepartmentService;
import com.chenxingnet.service.IPublicityService;
@Controller
@RequestMapping("/publicity")
public class PublicityController {

	
	@Resource
	IPublicityService   iPublicityService;
	
	@Resource
	DepartmentService  departmentService;
	
	//插入会议公示信息-跳转
	@RequestMapping("/addPublicitySkip")
	public String addPublicitySkip(HttpServletRequest request,Model model){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		List<Department>  deps=   departmentService.showDept();
		model.addAttribute("deps", deps);
		return "addMeet";
	}
	
	
	
	
	//插入会议公示信息
	@RequestMapping("/addPublicity")
	public String addPublicity(HttpServletRequest request,Model model){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		String  meetContext=request.getParameter("meetContext");
		String  depid=request.getParameter("deid");
		
		Publicity   meet=new  Publicity();
		meet.setPcontent(meetContext);
		meet.setPreportstart("1");
		meet.setPdate(new Date());
		int res = iPublicityService.insertSelectiveAndInsert(meet, depid);
		model.addAttribute("res", res);
		return "redirect:/back/guanli";
	}
	
	/**
	* @Title: /publicity/mobile_addPublicity 
	*移动端插入会议信息
	 */
	@RequestMapping("/mobile_addPublicity")
	public void mobile_addPublicity(HttpServletRequest request,Model model,HttpServletResponse response){
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			JSONObject returnJson = new JSONObject();
			returnJson.put("falg","true");
			try {
				String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
				if(rolename.equals("会计")){
					returnJson.put("access", "对不起，您无此操作权限");
				}
				Integer deptid = Integer.valueOf(request.getParameter("deptid"));
				if(deptid != null){
					returnJson.put("deptid", deptid);
				}
			} catch (Exception e) {
				
			}
			String  meetContext=request.getParameter("meetContext");
			String  depid=request.getParameter("deid");
			
			Publicity   meet=new  Publicity();
			meet.setPcontent(meetContext);
			meet.setPreportstart("1");
			meet.setPdate(new Date());
			int resu = iPublicityService.insertSelectiveAndInsert(meet, depid);
			
			returnJson.put("result",resu);//返回插入结果。
			response.getWriter().print(returnJson.toString());
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询会议全部信息
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/getMeetAll")
	public String getMeetAll(Model model,HttpServletRequest request){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
			if( (deptid != null && rolename.equals("部门经理")) || (deptid != null && rolename.equals("业务员")) ){
				List<meetVo> mvlist = iPublicityService.selectByDept(deptid);
				model.addAttribute("mvlist", mvlist);
				return "meet-tixing";
			}
			
		} catch (Exception e) {
			
		}
		List<meetVo> mvlist = iPublicityService.selectAll();
		model.addAttribute("mvlist", mvlist);
		return "meet-tixing";
		
	}
	
	
	/**
	* @Title: /publicity/mobile_getMeetAll 
	*移动端获取所有会议信息
	 */
	@RequestMapping("/mobile_getMeetAll")
	public void mobile_getMeetAll(HttpServletRequest request,HttpServletResponse response){
		try {
			response.setHeader("Access-Control-Allow-Origin", "*");
			response.setContentType("application/json;charset=utf-8");
			
			 JSONObject returnJson = new JSONObject();
			 returnJson.put("falg","true");
			List<meetVo> mvlist = iPublicityService.selectAll();//获取所有会议
			returnJson.put("mvlist", mvlist);
			response.getWriter().print(returnJson.toString());
		} catch (Exception e) {  e.printStackTrace();  }
	}
	
	
	
	
	@RequestMapping("/getMeetByid")
	public String getMeetAll(HttpServletRequest request ,Model model){
		try {
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(rolename.equals("会计")){
				model.addAttribute("result", "对不起，您无此操作权限");
				return "account_list";
			}
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
			if( (deptid != null && rolename.equals("部门经理")) || (deptid != null && rolename.equals("业务员")) ){
				Integer mid = Integer.valueOf(request.getParameter("meid"));
				meetVo mv = iPublicityService.selectByidAndDept(mid,deptid);
				model.addAttribute("meet", mv);
				return "meet-detail";
			}
			
			
			
		} catch (Exception e) {
			
		}
		Integer mid = Integer.valueOf(request.getParameter("meid"));
		meetVo mv = iPublicityService.selectByid(mid);
		model.addAttribute("meet", mv);
		return "meet-detail";
		
	}
	
	
	
}

